//
//  LeveyHUD.m
//  LeveyHUD
//
//  Created by yuike6 yuike6 on 12-2-15.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "LeveyHUD.h"
#import "LeveyHUDBottomMask.h"
#import "LeveyHUDTopMask.h"
#import "UIView+RectSize.h"

@interface LeveyHUD (private)
- (void)appearWithText:(NSString *)text;
- (void)disappear;
- (void)delayDisappear:(NSTimeInterval)delay withText:(NSString *)text;
@end


#define MASKOFFSET 30.0f

@implementation LeveyHUD

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        self.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5f];
        _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        [self addSubview:_spinner];
        _label = [[UILabel alloc] init];
        _label.textColor = [UIColor whiteColor];
        _label.textAlignment = UITextAlignmentCenter;
        _label.backgroundColor = [UIColor clearColor];
        _label.shadowColor = [UIColor blackColor];
        _label.shadowOffset = CGSizeMake(0, 1);
        _label.font = [UIFont boldSystemFontOfSize:16.0f];
        [self addSubview:_label];
        
        _topMask = [[LeveyHUDTopMask alloc] initWithFrame:CGRectMake(0, -30, frame.size.width, frame.size.height / 2.0f - 14)];
        _topMask.hidden = YES;
        [self addSubview:_topMask];
        _bottomMask = [[LeveyHUDBottomMask alloc] initWithFrame:CGRectMake(0, frame.size.height / 2.0f + 44.0f, frame.size.width, frame.size.height / 2.0f - 14)];
        _bottomMask.hidden = YES;
        [self addSubview:_bottomMask];
        
    }
        
    return self;
}

+ (LeveyHUD *)showHUDAddedTo:(UIView *)view withText:(NSString*)text {
    
    if (!view) {
        return nil;
    }
    
    LeveyHUD *hud = [[LeveyHUD alloc] initWithFrame:view.bounds];
    [view addSubview:hud];
    [hud appearWithText:text];
    
    return [hud autorelease];
 }
 
+ (LeveyHUD *)showHUDAddedToWindow:(UIWindow *) window  withText:(NSString*)text
{
    if (!window) {
        return nil;
    }
    
    LeveyHUD *hud = [[LeveyHUD alloc] initWithFrame:window.bounds];
    [window addSubview:hud];
    [hud appearWithText:text];
    
    return [hud autorelease];
}

+ (BOOL)hideHUDForView:(UIView *)view withText:(NSString*)text
{
    UIView *viewToRemove = nil;
	for (UIView *v in [view subviews]) 
    {
		if ([v isKindOfClass:[LeveyHUD class]]) {
			viewToRemove = v;
		}
	}
    
	if (viewToRemove != nil) 
    {
		LeveyHUD * HUD = (LeveyHUD *)viewToRemove;
        [HUD delayDisappear:1.0f withText:text];
		return YES;
	} 
    else 
    {
		return NO;
	}
}

+ (BOOL)hideHUDForWindow:(UIWindow *) window  withText:(NSString*)text
{
    UIView *viewToRemove = nil;
	for (UIView *v in [window subviews]) 
    {
		if ([v isKindOfClass:[LeveyHUD class]]) {
			viewToRemove = v;
		}
	}
	if (viewToRemove != nil) 
    {
		LeveyHUD * HUD = (LeveyHUD *)viewToRemove;
        [HUD delayDisappear:1.0f withText:text];
		return YES;
	} 
    else 
    {
		return NO;
	}
}

#pragma mark - Initialization
- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (unsigned)retainCount
{
    return UINT_MAX;  //denotes an object that cannot be released
}
- (oneway void)release
{
    
}

- (id)autorelease
{
    return self;
}
- (void)dealloc
{
    [_topMask release];
    [_bottomMask release];
    [_label release];
    [super dealloc];
}

#pragma mark - view's methods
- (void)layoutSubviews
{
    [super layoutSubviews];
    _label.frame = CGRectMake(10/*和转圈保持适当的距离*/, self.center.y - 15, self.bounds.size.width, 30);
}

#pragma mark - instant methods
- (void)appearWithText:(NSString *)text
{
    float x =  (self.bounds.size.width - [text sizeWithFont:_label.font].width)/2;
    CGSize size = _spinner.frame.size; // 不改变它的大小，否则模糊不清。。。
    _spinner.frame = [UIView autoAdjustRect:CGRectMake(x - 38, self.center.y - 20.0f, 40, 40) 
                                     toSize:size];
    _label.text = text;
    if (self.hidden == NO)
    {
        return;
    }
    
    self.hidden = NO;
    _topMask.alpha = 0.0f;
    _bottomMask.alpha = 0.0f;
    _topMask.hidden = NO;
    _bottomMask.hidden = NO;
    self.alpha = 0.0f;
    _label.hidden = YES;
    [UIView animateWithDuration:.3f animations:^{
        self.alpha = 1.0f;
        _topMask.alpha = 1.0f;
        _bottomMask.alpha = 1.0f;
        _topMask.frame = CGRectOffset(_topMask.frame, 0, MASKOFFSET);
        _bottomMask.frame = CGRectOffset(_bottomMask.frame, 0, -MASKOFFSET);
    } completion:^(BOOL finished) {
        if (finished) {
            [_spinner startAnimating];
            _label.hidden = NO;
        }
    }];
}
- (void)disappear
{
    if (self.hidden == YES)
    {
        return;
    }
    [UIView animateWithDuration:.3f animations:^{
        self.alpha = 0.0f;
        _topMask.alpha = 0.0f;
        _bottomMask.alpha = 0.0f;
        _topMask.frame = CGRectOffset(_topMask.frame, 0, -MASKOFFSET);
        _bottomMask.frame = CGRectOffset(_bottomMask.frame, 0, MASKOFFSET);
    } completion:^(BOOL finished) {
        if (finished) {
            _topMask.hidden = YES;
            _bottomMask.hidden = YES;
            [_spinner stopAnimating];
            _label.text = @"";
            self.hidden = YES;
            [self removeFromSuperview];
        }
    }];    
}

- (void)delayDisappear:(NSTimeInterval)delay withText:(NSString *)text
{
    [_spinner stopAnimating];
    _label.text = text;
    [self performSelector:@selector(disappear) withObject:nil afterDelay:delay];
}

@end
